package com.example.aboutspringboot.controller;


import com.example.aboutspringboot.entity.*;
import com.example.aboutspringboot.mapper.UserMapper;
import com.example.aboutspringboot.service.OrderService;
import com.example.aboutspringboot.service.ShopCarService;
import com.example.aboutspringboot.service.ShopService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("shopCar")
@CrossOrigin//跨域
public class ShopCarController {

    @Resource
    private ShopCarService shopCarService;
    @Resource
    private ShopService shopService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private OrderService orderService;

    //按用户名查购物车
    @GetMapping("/selectShopCar")
    public Map<String,Object> selectShopCar(String username){
        Map<String, Object> map = new LinkedHashMap<>();
        List<ShopCar> list= shopCarService.selectShopCar(username);
        for (ShopCar shopCar:list){
            shopCar.setImgSrc(shopService.selectImg(shopCar.getId()));
        }
        map.put("shop",list);
        return map;
    }

    //添加购物车
    @PostMapping("/insertShopCar")
    public Map<String,Object> insertShopCar(@RequestBody ShopCar shopCar){
        Map<String, Object> map = new LinkedHashMap<>();
//        System.out.println(shopCar);
        int num=shopCarService.insertShopCar(shopCar);
        if (num>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }

    //修改商品数量
    @PostMapping("/updateShopCar")
    public Map<String,Object> updateShopCar(@RequestBody ShopCar shopCar){
        Map<String, Object> map = new LinkedHashMap<>();
//        System.out.println(shopCar);
        int num=shopCarService.updateShopCar(shopCar);
        if (num>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }

    //商品数目减到零，执行删除商品操作
    @GetMapping("/deleteShopCarByNum")
    public Map<String,Object> deleteShopCarByNum(String username,long id){
        Map<String, Object> map = new LinkedHashMap<>();
        int num=shopCarService.deleteShopCarByNum(username, id);
        if (num>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }

    //结账
    @PostMapping("/submitShopping")
    public Map<String,Object> submit(@RequestBody Order order){
        Map<String, Object> map = new LinkedHashMap<>();
//        System.out.println(order);
        User user=userMapper.selectOneUserByUsername(order.getUsername());
        //没有足够的钱支付
        if (user.getMoney()<order.getAll()){
            map.put("result",false);
        }else {
            user.setMoney(user.getMoney()-order.getAll());  //扣钱
            userMapper.updateOneUser(user);
            //清空购物车
            shopCarService.deleteShopCar(order.getUsername());

            //创建订单
            orderService.insertOrder(order);
            long orderId=orderService.selectOrderItemId(order.getUsername(),order.getDate());
            for (OrderItem orderItem:order.getOrderItems()){
                orderItem.setId(orderId);
                orderService.insertOrderItem(orderItem);
            }
            map.put("result",true);
        }
        return map;
    }

}
